package com.example.administrator.test.order;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.test.R;
import com.example.administrator.test.login.RecordSQLiteOpenHelper;
import com.example.administrator.test.order.bean.CancelOrderSend;
import com.example.administrator.test.order.bean.CancelOrderTurn;
import com.google.gson.Gson;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import utils.PercentageRing;

/**
 * Created by Administrator on 2017/1/7.
 */
public class WaiteActivity extends AppCompatActivity {
    private PercentageRing mPercentageRing;
    private ImageView mWaiteOrderBack;
    private Button mWaiteOrderCancel;
    private RecordSQLiteOpenHelper helper = new RecordSQLiteOpenHelper(this);
    private SQLiteDatabase db;
    private String token;//获取登录密匙
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_waite);
        //对数据库进行查询
        db=helper.getReadableDatabase();
        Cursor cursor = db.query("records", null, null, null, null, null, null);
        //如果数据库不为空
        if (cursor!=null&& cursor.moveToFirst()){
            token = cursor.getString(cursor.getColumnIndex("token"));
        }
        mPercentageRing= (PercentageRing) findViewById(R.id.circle);
        mWaiteOrderBack= (ImageView) findViewById(R.id.waitOrderBack);
        mWaiteOrderCancel= (Button) findViewById(R.id.waiteOrderCancel);
        //设置目标百分比为30
        mPercentageRing.setTargetPercent(100);
        mWaiteOrderBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mWaiteOrderCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(WaiteActivity.this,CancelReasonActivity.class);
                startActivity(intent);
                CancelOrderSend cancelOrderSend=new CancelOrderSend();
                cancelOrderSend=initTiJiaoDingDan(cancelOrderSend);
                Gson gson=new Gson();
                String jsonString=gson.toJson(cancelOrderSend);
                RequestBody body=RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),jsonString);
                Retrofit retrofit=OrderWay.getRetriofit();
                PostDingDan postDingDan=retrofit.create(PostDingDan.class);
                Call<CancelOrderTurn> call=postDingDan.getCancelOrderTurn(body);
                call.enqueue(new Callback<CancelOrderTurn>() {
                    @Override
                    public void onResponse(Call<CancelOrderTurn> call, Response<CancelOrderTurn> response) {
                        CancelOrderTurn cancelOrderTurn=response.body();
                        String description=cancelOrderTurn.getDescription();
                        String resultCode=cancelOrderTurn.getResultCode();
                        String cancelState= cancelOrderTurn.getCancelState();
                        Log.d("tag","description:"+description+"resultCode:"+resultCode+"cancelState:"+cancelState);
                    }

                    @Override
                    public void onFailure(Call<CancelOrderTurn> call, Throwable t) {

                    }
                });
            }
        });
    }
    public CancelOrderSend initTiJiaoDingDan(CancelOrderSend cancelOrderSend){
        cancelOrderSend.setOperType("2007");
        cancelOrderSend.setToken(token);
        cancelOrderSend.setOrderNumber("66666666666666666666666");
        cancelOrderSend.setCancelReason("没有理由");
        return cancelOrderSend;
    }
}
